package client;


import client.Configuration;

import java.awt.EventQueue;
import java.awt.HeadlessException;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;

import server.BkCAccITF;
import server.PersonInterface;

import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;



public class BkCurrentAccountGUI extends JFrame {

	private JPanel contentPane;

	/**
	 * Launch the application.
	 */
	
	Person ps = new Person();
	int pID = ps.getPID();
		
	Configuration config=new Configuration();
	String rmiAddress=config.getRmiAddress();
	BkCAccITF CAitf;


	
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					BkCurrentAccountGUI frame = new BkCurrentAccountGUI();
					frame.setVisible(true);
					}
				catch (Exception e) {
					e.printStackTrace();
					}
				}
			});
		}
	/**
	 * Create the frame.
	 */

	  
	public BkCurrentAccountGUI() {
		setTitle("\u6D3B\u671F\u8D26\u6237");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		
		JButton button = new JButton("\u4F59\u989D\u67E5\u8BE2");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				try {
					CAitf = (BkCAccITF) Naming.lookup(rmiAddress+"BankCurrentAccount");
					
					JOptionPane.showMessageDialog(null, "您的余额为："+CAitf.checkBalance(pID));
					} catch (HeadlessException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
						} catch (RemoteException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
							}catch (MalformedURLException e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
								} catch (NotBoundException e1) {
									// TODO Auto-generated catch block
									e1.printStackTrace();
									}
				}
			});
		
		button.setFont(new Font("微软雅黑", Font.PLAIN, 17));
		button.setBounds(66, 86, 123, 67);
		contentPane.add(button);
		
		JButton button_1 = new JButton("\u8F6C\u8D26");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					String stranNum = JOptionPane.showInputDialog("请输入将转入的活期账户号："); 
					if(stranNum.equals(""))
						JOptionPane.showMessageDialog(null, "输入不能为空！");
					else{
						int tranNum = Integer.parseInt(stranNum);
						String stranSum = JOptionPane.showInputDialog("请输入转账金额："); 
						double tranSum=Double.parseDouble(stranSum);
					
						CAitf = (BkCAccITF) Naming.lookup(rmiAddress+"BankCurrentAccount");
						if(CAitf.checkBalance(pID)<tranSum)
								JOptionPane.showMessageDialog(null, "对不起！您的存储账户余额不足");
						else if(tranSum<=0)
								JOptionPane.showMessageDialog(null, "您的输入有误！请重新输入");
						else if(CAitf.transfer(pID,tranNum,tranSum)==0)
								JOptionPane.showMessageDialog(null, "转账成功！");
					}
			} catch (HeadlessException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (RemoteException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (MalformedURLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (NotBoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			}
		});
		button_1.setFont(new Font("微软雅黑", Font.PLAIN, 15));
		button_1.setBounds(249, 156, 123, 47);
		contentPane.add(button_1);
		
		JButton button_2 = new JButton("\u5B58\u6B3E");
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String sCAin = JOptionPane.showInputDialog("请输入存款额：");  
				double CAin=Double.parseDouble(sCAin);
				
				try {
					CAitf = (BkCAccITF) Naming.lookup(rmiAddress+"BankCurrentAccount");
					PersonInterface Person = (PersonInterface) Naming.lookup(rmiAddress+"Person");
					if(CAin>Person.getCash(pID))
						JOptionPane.showMessageDialog(null, "现金不足！");
					else if(CAitf.deposit(pID, CAin)==1)
						JOptionPane.showMessageDialog(null, "您的输入有误！请重新输入");
					else 
						JOptionPane.showMessageDialog(null, "存款成功！");
					//cash减少
				} catch (HeadlessException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (RemoteException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (MalformedURLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (NotBoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		button_2.setFont(new Font("微软雅黑", Font.PLAIN, 15));
		button_2.setBounds(249, 38, 123, 47);
		contentPane.add(button_2);
		
		JButton button_3 = new JButton("\u53D6\u6B3E");
		button_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String sCAout = JOptionPane.showInputDialog("请输入取款额：");  
				double CAout=Double.parseDouble(sCAout);
				try {
					CAitf = (BkCAccITF) Naming.lookup(rmiAddress+"BankCurrentAccount");
				
				if(CAitf.withdrawl(pID, CAout)==1)
					JOptionPane.showMessageDialog(null, "对不起！存储账户余额不足");
					
				else if(CAout<=0)
					JOptionPane.showMessageDialog(null, "您的输入有误！请重新输入");
				else
					JOptionPane.showMessageDialog(null, "请取现金！谢谢");
				     //cash增加
				} 
				catch (MalformedURLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (RemoteException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (NotBoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		button_3.setFont(new Font("微软雅黑", Font.PLAIN, 15));
		button_3.setBounds(249, 97, 123, 47);
		contentPane.add(button_3);
		
		JButton button_4 = new JButton("\u8FD4\u56DE");
		button_4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				BankAccountGUI frame = new BankAccountGUI();
				frame.setLocationRelativeTo(null);
				frame.setVisible(true);
				dispose();
			}
		});
		button_4.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		button_4.setBounds(341, 6, 83, 22);
		contentPane.add(button_4);
	}

}
